package com.bzkj.basicinfo.service.impl.yxl;

import com.bzkj.basicinfo.domain.wms.StoStockEntity;
import com.bzkj.basicinfo.domain.yxl.YxlBillsList;
import com.bzkj.basicinfo.domain.yxl.vo.ExportOutBillsList;
import com.bzkj.basicinfo.domain.yxl.vo.ExportSalableDrugVo;
import com.bzkj.basicinfo.domain.yxl.vo.ExportStoStockVo;
import com.bzkj.basicinfo.mapper.YxlStoStockMapper;
import com.bzkj.basicinfo.service.yxl.YxlStoStockService;
import com.bzkj.common.annotation.DataSource;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.enums.DataSourceType;
import com.bzkj.common.utils.DataProcessCommonUtils;
import com.bzkj.common.utils.poi.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author：swj
 * @Desc
 * @Date：2025/7/28 10:28
 */
@Service
public class YxlStoStockServiceImpl implements YxlStoStockService {

    @Autowired
    private YxlStoStockMapper yxlStoStockMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<StoStockEntity> queryBasicInfo(StoStockEntity entity) {
        return yxlStoStockMapper.selectBasicInfo(entity);
    }

    @Override
    public List<StoStockEntity> queryStoStockInfo(StoStockEntity entity) {
        return yxlStoStockMapper.selectStockInfo(entity);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public AjaxResult exportStoStockInfo(StoStockEntity entity, HttpServletResponse response) {
        List<StoStockEntity> list = yxlStoStockMapper.selectStockInfo(entity);
        List<ExportStoStockVo> voList = list.stream()
                .map(stockEntity -> {
                    ExportStoStockVo vo = new ExportStoStockVo();
                    BeanUtils.copyProperties(stockEntity, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        ExcelUtil<ExportStoStockVo> util = new ExcelUtil<>(ExportStoStockVo.class);
        util.exportExcel(response, voList, "库存盘点", "库存盘点", "库存盘点");
        return AjaxResult.success();
    }

    @Override
    public AjaxResult queryWmsStockLog(String operator, String operateType, String batchCode, String productName,
                                       String manufacturer, String beginDate, String endDate, Integer pageNum, Integer pageSize) {
        beginDate = beginDate + " 00:00:00";
        endDate = endDate + " 23:59:59";
        List<HashMap<String, Object>> mapList = yxlStoStockMapper.queryWmsStockLog(operator, operateType, batchCode, productName, manufacturer, beginDate, endDate);
        return AjaxResult.success(DataProcessCommonUtils.getTableInfo(mapList, pageNum, pageSize));
    }
}
